<template>
  <div>
    <el-button type="primary" size="default" @click="handleCategory(1)">
      获取梵几家具
    </el-button>
    <el-button type="primary" size="default" @click="handleCategory(2)">
      获取梵几家居
    </el-button>

    <el-button type="primary" size="default" @click="handleHospital">
      获取医院数据
    </el-button>

    <el-button type="primary" size="default" @click="handlePost">
      获取post接口数据
    </el-button>
    <!-- 主分类菜单 -->
    <el-tabs v-model="activeName" class="demo-tabs" @tab-click="handleClick">
      <el-tab-pane label="家具" name="first"></el-tab-pane>
      <el-tab-pane label="家居" name="second"></el-tab-pane>
    </el-tabs>
    <!-- 子分类列表 -->
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="name" label="分类名称" width="150" />
      <el-table-column prop="fnjiType" label="主分类编号" width="150" />
      <el-table-column prop="value" label="子类目编码" />
      <el-table-column label="操作">
        <template #default="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">
            编辑
          </el-button>
          <el-button
            size="small"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
          >
            删除
          </el-button>
          <el-button :disabled="scope.row.saveType" type="primary" size="small" @click="handleProductSave(scope.row,scope.$index)">
            {{scope.row.saveType==1?'转存完毕':'点击转存'}}
          </el-button>
          
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script setup>
import { ref } from "vue";
import {
  categoryBatch,
  categoryGet,
  dayiHospitalGet,
  fnjiCategoryGet,
  testPost,
} from "../../api/category";
import { fnjiProductGet } from "../../api/product";
const handleCategory = async (type) => {
  let res = await fnjiCategoryGet(type); //1.获取第三方数据
  console.log(res); //从res中提取自己需要转存的部分数据包
  await categoryBatch(res.data.data, type); //2. 转存数据到LeanCloud
};

const handleHospital = async () => {
  let res = await dayiHospitalGet();
  console.log(res);
};

const handlePost = async () => {
  let res = await testPost();
  console.log(res);
};

//分类交互
const handleClick = async (tab) => {
  // console.log(tab.index+1)  //默认是字符串拼接
  let type = Number(tab.index) + 1;
  console.log(type);
  let res = await categoryGet(type); //携带不同参数，请求category分类表
  console.log(res);
  tableData.value = res.data.results; //将获取到的不同分类数据展示在表格中
};
const tableData = ref([
  {
    fnjiType: 1,
    name: "桌子",
    value: "C01",
  },
]);
const activeName = ref("first");

//首屏数据渲染
let res = await categoryGet(1); //直接放在setup顶层的await，必须配合Suspense
console.log("首屏数据", res);
tableData.value = res.data.results;

//转存分类商品
const handleProductSave = async (row,index)=>{
  // console.log(row,index);  //row是被点击这一行的数据包对象，index是序号
  await fnjiProductGet(
    row.value,   //子分类
    row.fnjiType,  //主分类
    row.objectId  //分类id
  )
  //更新本地分类数据包中的saveType
  tableData.value[index].saveType = 1
}
</script>

<style lang="scss" scoped></style>
